﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.admin.shop
{
    public partial class shop_list : System.Web.UI.Page
    {
        YXShop.BLL.Shop.Shop bll = new YXShop.BLL.Shop.Shop();
        protected void Page_Load(object sender, EventArgs e)
        {
            YXShop.Common.PromptInfo.Popedom("013003001");

            if(!IsPostBack)
            {
                if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty && ChangeHope.WebPage.PageRequest.GetFormString("id") != "")
                {
                    
                    string types = Request["Option"].Trim();
                    string id = ChangeHope.WebPage.PageRequest.GetFormString("id");
                    if (types == "del")
                    {
                        if (YXShop.Common.PromptInfo.Message("013003003") != "ok")
                        {
                            bll.Delete(Convert.ToInt32(id));
                        }
                        else
                        {
                            Response.Write("no");
                        }
                    }
                    if (types == "delAll")
                    {
                        if (YXShop.Common.PromptInfo.Message("013003003") != "ok")
                        {
                        bll.DeleteAll(id);
                         }
                        else
                        {
                            Response.Write("no");
                        }
                    }
                    Response.End();
                    return;
                }
                this.lblList.Text = GetList("");
            }
        }

        #region 绑定
        private string GetList(string where)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            ChangeHope.DataBase.DataByPage dataPage = bll.GetList(where);
            table.AddHeadCol("", "<input id=\"chall\" type=\"checkbox\" onclick=\"SelectAll(chall)\" />全选");
            table.AddHeadCol("","店标");
            table.AddHeadCol("","店铺名称");
            table.AddHeadCol("","店铺类型");
            table.AddHeadCol("","主营商品");
            table.AddHeadCol("","用户名称");
            table.AddHeadCol("","开始时间");
            table.AddHeadCol("","结束时间");
            table.AddHeadCol("","审核状态");
            table.AddHeadCol("","操作");
            table.AddRow();
            if (dataPage.DataReader != null)
            {
                int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                if (curpage < 0)
                {
                    curpage = 1;
                }
                int count = 0;
                YXShop.Common.SysParameter sp=new YXShop.Common.SysParameter();

                while (dataPage.DataReader.Read())
                {                   
                    count++;
                    string No = (15 * (curpage - 1) + count).ToString();
                    table.AddCol("<input id=\"cbTm\"  type=\"checkbox\" value=\"" + dataPage.DataReader["sid"].ToString() + "\" />");
                    table.AddCol("<img  src=\"" + sp.DummyPaht + dataPage.DataReader["shopsign"].ToString() + "\" width=\"40px\" height=\"40px\"  alt=\"\" />");
                    table.AddCol(dataPage.DataReader["shopname"].ToString());
                    table.AddCol(GetShopType(Convert.ToInt32(dataPage.DataReader["shoprank"])));
                    table.AddCol(GetShopCategory(Convert.ToInt32(dataPage.DataReader["categories"])));
                    table.AddCol(GetUserName(Convert.ToInt32(dataPage.DataReader["uid"])));
                    table.AddCol(dataPage.DataReader["startdate"].ToString());
                    table.AddCol(dataPage.DataReader["enddate"].ToString());
                    table.AddCol(dataPage.DataReader["state"].ToString()=="0"?"未审核":"已审核");
                    string strUrl = YXShop.TemplateAction.Common.UrlStr("shop_edit", "shopId=" + dataPage.DataReader["sid"].ToString(), "admin/shop/");
                    table.AddCol("<a href=\""+strUrl+"\">查看/修改</a>&nbsp;<a href=\"#\" onclick='Del("+dataPage.DataReader["sid"]+")'>删除</a>");
                    table.AddRow();
                }
              
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }
        #endregion

        #region  获取数据
        /// <summary>
        /// 店铺类型
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        private string GetShopType(int typeId)
        {
            string reStr = "f";
            YXShop.BLL.Shop.ShopLevel levelbLL = new YXShop.BLL.Shop.ShopLevel();
            YXShop.Model.Shop.ShopLevel levelmodel = levelbLL.GetModelByID(typeId);
            if (levelmodel != null)
            {
                reStr = levelmodel.Name != null ? levelmodel.Name : "";
            }
            return reStr;
        }
        /// <summary>
        /// 主营
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        private string GetShopCategory(int cateId)
        {
            YXShop.BLL.Product.Productclass classBll = new YXShop.BLL.Product.Productclass();
            YXShop.Model.Product.Productclass model = classBll.GetModelID(cateId);
            if (model != null)
            {
                return model.Name.ToString();
            }
            else
            {
                return "";
            }

        }
        /// <summary>
        /// 用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        private string GetUserName(int userId)
        {
            string url = YXShop.TemplateAction.Common.UrlStr("member_view", "uid=" + userId, "admin/member/");
            YXShop.BLL.Member.MemberAccount userBll = new YXShop.BLL.Member.MemberAccount();
            YXShop.Model.Member.MemberAccount user = userBll.GetModel(userId);
            if (user != null)
            {
                return "<a href=\""+url+"\">" + user.UserId + "</a>";
            }
            else
            {
                return "";
            }

        }

        #endregion

        #region 查询
        protected void linkQuery_Click(object sender, EventArgs e)
        {
            bool flag = false;
            for (int i = 0; i < ChbWork.Items.Count; i++)
			{
			    if(ChbWork.Items[i].Selected)
                {
                    flag = true;
                }
			}
            string strWhere = string.Empty;
            if(flag)
            {
                strWhere = " and "+ GetKeyValue(ChbWork); 
            }                
            this.lblList.Text = GetList(strWhere);
        }
        #endregion

        #region 获取查询条件
        private string GetKeyValue(CheckBoxList cbl)
        {
            string reStr = "";
            int w1 = -1;
            string w2 = "";        
            if (cbl.Items.Count == 7)
            {
                for (int i = 0; i < cbl.Items.Count; i++)
                {
                    if (i == 3)
                    {
                        w1 = cbl.Items[i].Selected ? 1 : 0;                      
                        continue;
                    }
                    else
                    {
                        w2 += cbl.Items[i].Selected ? "1," : "[0,1],"; 
                    }
                }
                w2 = w2.TrimEnd(',');
            }
            reStr = string.Format(" state ={0} and attribute like '{1}'", w1, w2);
            return reStr;
        }
        #endregion
    }
}
